package com.ambitor.ipro.actions;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.ambitor.grass.util.data.DataMap;
import com.ambitor.grass.util.data.DatasetList;
import com.ambitor.grass.util.data.IData;
import com.ambitor.grass.util.data.IDataset;
import com.ambitor.ipro.exceptions.ProException;
import com.ambitor.ipro.service.TopService;
import com.ambitor.ipro.util.Constants;
import com.ambitor.ipro.util.PropertiesReader;
import com.ambitor.ipro.util.StringUtil;
import com.taobao.api.domain.ItemCat;
import com.taobao.api.domain.ItemCategory;
import com.taobao.api.domain.ItemSearch;
import com.taobao.api.domain.SellerCat;

public class CategoryAction extends BaseAction {

	private static final long serialVersionUID = 8189262641616011463L;

	private IDataset sellerCats = new DatasetList();

	private List<ItemCat> itemCats = new ArrayList<ItemCat>();

	@SuppressWarnings("unchecked")
	public String queryCats() {
		TopService topService = new TopService(Constants.EVN_SWITCH,
				Constants.APP_KEY_IPROMOTION, Constants.APP_SECRET_IPROMOTION);
		String nick = pd.getTopNick();
		try {
			ItemSearch itemSearch = topService.getCatIds(nick);
			List<ItemCategory> itemCids = itemSearch.getItemCategories();

			String cids = "";
			for (ItemCategory itemCategory : itemCids) {
				cids += itemCategory.getCategoryId() + ",";
			}
			cids = StringUtil.trimSuffix(cids, ",");
			itemCats = topService.getCats(cids, nick);
		} catch (ProException e) {
			logger.warn(nick + "获取分类出现异常");
		}
		return SUCCESS;
	}

	@SuppressWarnings("unchecked")
	public String querySellerCats() {
		TopService topService = new TopService(Constants.EVN_SWITCH,
				Constants.APP_KEY_IPROMOTION, Constants.APP_SECRET_IPROMOTION);
		String nick = pd.getTopNick();
		List<SellerCat> cats = null;
		try {
			cats = topService.getSellerCats(nick);
		} catch (ProException e) {
			logger.warn(nick + "获取分类出现异常");
		}
		if (cats == null || cats.size() == 0)
			return SUCCESS;
		_processSellerCats(cats);
		logger.info("getSellerCats取出" + sellerCats.size() + "条数据.");
		return SUCCESS;
	}

	/**
	 * 整理成树形结构
	 * 
	 * @param cats
	 */
	@SuppressWarnings("unchecked")
	private void _processSellerCats(List<SellerCat> cats) {
		for (SellerCat cat : cats) {
			if (cat.getParentCid().intValue() == 0 && _validateCatName(cat)) { // 遍历第一级类别
				IData data = new DataMap();
				data.put("cid", cat.getCid());
				data.put("name", cat.getName());
				data.put("isLeaf", "1");
				sellerCats.add(data);
				IDataset subCatList = new DatasetList();
				for (SellerCat subCat : cats) {
					if (subCat.getParentCid().intValue() != 0
							&& subCat.getParentCid().equals(cat.getCid())
							&& _validateCatName(cat)) { // 寻找子分类
						IData subData = new DataMap();
						subData.put("cid", subCat.getCid());
						subData.put("name", subCat.getName());
						subData.put("isLeaf", "2");
						subCatList.add(subData);
					}
				}
				if (subCatList.size() > 0) {
					data.put("isLeaf", "0");
					sellerCats.addAll(subCatList);
				}
			}
		}
	}

	/**
	 * 把一些应用建的统计用的类目过滤掉
	 * 
	 * @param cat
	 * @return
	 */
	private boolean _validateCatName(SellerCat cat) {
		String invalidCats = PropertiesReader.getString("INVALID_CATS_NAME");
		for (String str : invalidCats.split(",")) {
			if (StringUtils.contains(cat.getName(), str)) {
				return false;
			}
		}
		return true;
	}

	public IDataset getSellerCats() {
		return sellerCats;
	}

	public void setSellerCats(IDataset sellerCats) {
		this.sellerCats = sellerCats;
	}

	public List<ItemCat> getItemCats() {
		return itemCats;
	}

	public void setItemCats(List<ItemCat> itemCats) {
		this.itemCats = itemCats;
	}

}
